package hu.issue.web;

import hu.issue.ejb.ProjectService;
import hu.issue.entity.Project;
import hu.issue.framework.web.Constants;
import hu.issue.framework.web.controller.Inject;
import hu.issue.framework.web.controller.RequiredRole;
import hu.issue.framework.web.controller.SimpleFormController;
import hu.issue.framework.web.validation.ErrorMessages;
import hu.issue.framework.web.validation.NotEmptyValidator;
import hu.issue.framework.web.validation.RequestValidation;
import hu.issue.framework.web.validation.RequiredValidator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredRole(Constants.ROLE_PROJECTOWNER)
public class NewProjectController extends SimpleFormController {
	@Inject
	private ProjectService projectService;

	@Override
	protected boolean handleSubmissionInternal(final HttpServletRequest request, final HttpServletResponse response, final ErrorMessages messages) throws Exception {
		final String name = request.getParameter("name");
		String description = request.getParameter("description");
		if (description == null) {
			description = "";
		}

		this.projectService.createProject(new Project(name, description));
		return true;
	}

	@Override
	protected String getSuccessView() {
		return "redirect:dashboard.do";
	}

	@Override
	protected void buildValidation(final RequestValidation validation) {
		validation.addValidator(new RequiredValidator("name", "Projekt név kötelező"));
		validation.addValidator(new NotEmptyValidator("name", "Projekt név nem lehet üres"));
	}

}
